import java.awt.AWTException;
import java.awt.Color;
import java.awt.HeadlessException;

import java.awt.Graphics;
import javax.swing.*;

import java.awt.*;
import java.awt.event.MouseEvent;
import java.awt.event.MouseMotionAdapter;
import java.awt.geom.*;
import java.awt.event.*;
import java.io.*;
import java.awt.*;
import javax.imageio.ImageIO;



import java.util.*;


import java.awt.image.BufferedImage;
import java.awt.print.PageFormat;
import java.awt.print.Printable;
import java.awt.print.PrinterException;
import java.awt.print.PrinterJob;

public class TinCDCanvas extends JPanel implements MouseListener, MouseMotionListener{
	
	
	Point cursorLocation;
	int x = 0;
	int y = 0;
	int oldX,oldY;
	final double PI = 3.14159265358979323846;
	Line2D.Double NULL = new Line2D.Double(0,0,0,0);
	boolean drawSavedItem = false;
	int panelHeight = 0;
	int panelWidth = 0;
	int newInstance = 0;
	double slope, inverseSlope, ratioX, ratioY, ratio, tempNum, tempNum2;
	private MainFrame TinCDCopy;
	
	private Color backgroundColor = new Color(0.0f,0.0f,0.0f);
	
	TinCDCanvas(MainFrame TinCD)
	{
		TinCDCopy = TinCD;
		addMouseMotionListener(this);
		addMouseListener(this);
		
		
		//Carbon carbon = new Carbon();
		//Hydrogen hydrogen = new Hydrogen();
		//AtomicBond atomicbond = new AtomicBond(carbon,hydrogen);
	
	}
	
	

	public void paintComponent(Graphics g)
	{
		super.paintComponent(g);
		
		//setPanel dimensions
		setCanvasDim();	
		Graphics2D gg = (Graphics2D)g;
		g.setColor(backgroundColor);
		g.fillRect(0, 0, panelWidth, panelHeight);
		
		//Sets the skeletal form lines to black
		
		g.setColor(new Color(0,0,0));
		
		
		gg.setPaint(Color.WHITE);
		gg.setRenderingHint(RenderingHints.KEY_ANTIALIASING,RenderingHints.VALUE_ANTIALIAS_ON);
		gg.setStroke(new BasicStroke(1.4f, BasicStroke.CAP_ROUND,BasicStroke.JOIN_ROUND));
		
		//drawALine(gg);
		gg.drawLine(50,50,50+30,50+30);
		//gg.drawString("Brian Loves Raina!", 200, 200);
		
		
	}
	
	void testPrinter()
	{
		 
		
	}
	void setCanvasDim()
	{
		panelHeight = TinCDCopy.getHeight();
		panelWidth = TinCDCopy.getWidth();
	}
	void drawALine(Graphics2D gg)
	{
		if(drawSavedItem == true)
		{
			
			ratioX = slope*30;
			System.out.println(ratioX);
			ratioY = inverseSlope*30;
			System.out.println(ratioY);
			
			if((x-oldX) > (y-oldY))
				{	
					ratio = ratioY/ratioX;
					System.out.println(ratio);
				}
			
			else
				{	ratio = ratioX/ratioY;
			    	System.out.println(ratio);
				}
			tempNum = Math.sqrt((1800/(1/ratio)));
			tempNum2 = Math.sqrt((1800-tempNum));
			
			if(ratioX > ratioY)
			{
		       //gg.drawLine(oldX,oldY,oldX+(int)tempNum,oldY+(int)tempNum2);
			}
			//else
				//gg.drawLine(oldX,oldY,oldX+(int)tempNum2,oldY+(int)tempNum);
			
		}
		
	
		repaint();
	}
	
	void clearJPanel()
	{
		
		repaint();
	}
	
	public void setBackgroundColor(Color color)
	{
		backgroundColor = color;
		repaint();
	}

	
	@Override
	public void mouseClicked(MouseEvent arg0) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void mouseEntered(MouseEvent arg0) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void mouseExited(MouseEvent arg0) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void mousePressed(MouseEvent e) {
		
		if(e.getModifiers() == e.BUTTON1_MASK)
		{
			drawSavedItem = true;
			oldX = (int)(e.getPoint().getX()) - (int)Math.sqrt(900);
			oldY = (int)(e.getPoint().getY()) - (int)Math.sqrt(900);
			
			
			
			
			
			
			repaint();
		
			//mainHandle.addMessage("You selected: " + selected);	//delete this
		}
		
	}

	@Override
	public void mouseReleased(MouseEvent e) {
		
		if(e.getModifiers() == e.BUTTON1_MASK)
		{
			drawSavedItem = false;
			
			repaint();
		
			//mainHandle.addMessage("You selected: " + selected);	//delete this
		}
		
	}

	@Override
	public void mouseDragged(MouseEvent e) {
		// TODO Auto-generated method stub
		
		
		x = (int)(e.getPoint().getX());
		y = (int)(e.getPoint().getY());
	}

	@Override
	public void mouseMoved(MouseEvent e) {
	
		x = (int)(e.getPoint().getX());
		y = (int)(e.getPoint().getY());
		
		
		slope = (double)x/y;
		inverseSlope = (double)y/x;

		repaint();
		
	}
	
	public void testScreen(File file, String extension) throws IOException, AWTException
	{
		
		try {

			BufferedImage image = new BufferedImage(panelWidth, panelHeight, BufferedImage.TYPE_INT_RGB);

			Graphics2D g = image.createGraphics();

			super.paint(g);

			ImageIO.write(image, extension , new File(file.getAbsolutePath()+"."+extension));
			
			}
			catch(Exception e) {}
		
		
			
		
	}



	
	
	
}
